library(knitr)
## Warning: package 'knitr' was built under R version 3.5.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.5.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

Visualising the political preferences

Questionaire

Brace yourself! Polish Parliamentary Election is coming October 13!

In Poland, as I guess everywhere, politics are promising a lot and evade direct answers. Thus I really like the tools like http://latarnikwyborczy.pl, widely used by polish voters before the elections (unfortunately there is no English version). It allows the citizens to compare their views on most discussed issues with the answers given by political parties - so gives you the clear overview of politics opinions.

This year questionnarie consist of 20 questions. They’re not divided into groups. There are basically three answers to each question: “I agree”, “I have no opinion”, “I disagree”. The final result is percentage coverage of your’s and political parties’ answers. Currently we have five of them. Of course this not determine your vote, as there might be other reasons to back or do not support some groups, but this gives a nice general view.

As my job and hobby is to visualise data I feel that there might be a better way to present results then single percent. Let me show you in this short report two options that come to my mind.

Parties and questions

I guess it is high time to introduce our players! (in brackets the short names - made from polish names - used in plots legends; alphabetic order by short names)

  • Civic Coalition: former ruling party Civic Platform + Modern (Nowoczesna), current President of the European Council was it’s leader before nomination (KO);
  • Confederation Freedom and Independence: righ-wing and eurosceptic alliance, according to the polls they’re balancing on election 5% threshold (Konfederacja);
  • The Left: left-wing alliance formed by parties SLD + Wiosna + Razem (Lewica);
  • Polish Coalition: Polish People’s Party (traditional rural party) + former rock musician Paweł Kukiz - interesting mix (PSL);
  • Law and Justice: current ruling party, polls give it most chances to win again (PiS);

And the list of questions in order of appearance:

QUESTIONS <- list(
  question01 = "Q1: The scope of competence of local self-governments should be gradually expanded.",
  question02 = "Q2: The headquarters of some central offices should be moved outside of Warsaw to other cities throughout the country.",
  question03 = "Q3: The independence of the judiciary from parliament and government should be strengthened.",
  question04 = "Q4: Retirement age should be increased.",
  question05 = "Q5: The income tax-free amount should be increased significantly.",
  question06 = "Q6: A patient in a public healthcare system facility should be able to pay for a higher standard of medical services.",
  question07 = "Q7: The scope of theoretical knowledge taught in schools should be limited in favor for the development of students' skills.",
  question08 = "Q8: The priority of the state's cultural policy should be to strengthen national identity.",
  question09 = "Q9: Public funds for learning should be focused on supporting the best universities in the country.",
  question10 = "Q10: Information media should remain under the Polish financial control.",
  question11 = "Q11: Christian values should be the basis of the state's social policy.",
  question12 = "Q12: The abortion law should be relaxed.",
  question13 = "Q13: Same-sex people should be able to enter into a legal partnership.",
  question14 = "Q14: Coal should remain the primary source of energy in Poland.",
  question15 = "Q15: Poland should accelerate the construction of the nuclear power plant.",
  question16 = "Q16: The development and strengthening of Territorial Defense Forces should be continued.",
  question17 = "Q17: Poland should accept a larger number of economic immigrants from other countries.",
  question18 = "Q18: The European Union should have less influence on Polish internal policy.",
  question19 = "Q19: Poland should support the deepening of European integration in the field of foreign and defense policy.",
  question20 = "Q20: Poland should strive to maintain international sanctions imposed on Russia after the aggression on Ukraine."
)

I think you can more or less guess most of the answers given by each party, so you can compare it with the actual results now - but I strongly recommend to wait for more fancy solutions. I wrote down all the answers in simple csv file (manually, no fancy web-scraping this time) so now we can load them easily. To make my life easier in further computational methods I’ve coded the answers as follows: 1 - I agree 0 - I have no opinion -1 - I disagree

ANSWERS <- read.csv2("./app/data/answers.csv")
Party question01 question02 question03 question04 question05 question06 question07 question08 question09 question10
Lewica 1 1 1 -1 1 -1 1 -1 -1 -1
PSL 1 1 1 -1 1 1 1 1 -1 -1
Konfederacja 1 1 1 0 1 1 1 1 1 0
KO 1 1 1 -1 1 1 1 -1 -1 -1
PiS -1 1 -1 -1 0 -1 1 1 -1 1
Party question11 question12 question13 question14 question15 question16 question17 question18 question19 question20
Lewica -1 1 1 -1 0 -1 1 -1 1 1
PSL 1 -1 -1 -1 -1 -1 -1 1 1 1
Konfederacja 1 -1 -1 1 1 1 -1 1 -1 1
KO -1 -1 1 -1 -1 -1 1 0 1 1
PiS 1 -1 -1 1 1 1 -1 -1 1 1

Radar charts

Goal of the first solution is to present all the questions and answers on the single plot. The so called radar (or web) chart seems perfect for the job.

library(plotly)
## Warning: package 'plotly' was built under R version 3.5.3
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.5.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
df <- read.csv("https://raw.githubusercontent.com/bcdunbar/datasets/master/iris.csv")

p <- df %>%
  plot_ly(type = 'parcoords',
          line = list(color = ~species_id,
                      colorscale = list(c(0,'red'),c(0.5,'green'),c(1,'blue'))),
          dimensions = list(
            list(range = c(2,4.5),
                 label = 'Sepal Width', values = ~sepal_width),
            list(range = c(4,8),
                 constraintrange = c(5,6),
                 label = 'Sepal Length', values = ~sepal_length),
            list(range = c(0,2.5),
                 label = 'Petal Width', values = ~petal_width),
            list(range = c(1,7),
                 label = 'Petal Length', values = ~petal_length)
            )
          )
p

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.